/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 077.c
 * Poisson's Wine
 ***********************************************************************************************/

#include <stdio.h>

#define NEED 6

/*
 * divide 12pt wine in 12pt-bottle using 8pt and 5pt bottles
 * I don't know how to classify this problem, is it greedy?
 */

// move wine from a to a by a->b->c->a
// y is volume of b, z is volume of c
// a, b, c is current wine in each bottle
int move(int a, int y, int z)
{
    int b = 0, c = 0;
    printf("a:%2d b:%2d c:%2d\n%4d%4d%4d\n", a, y, z, a, b, c);
    while (a != NEED || b != NEED && c != NEED)
    // this estimation can be changed into while(none == NEED)
    // after find a NEED, we can just deal with the others
    {
        if (b == 0)
        // if b is empty, fill b with a
        // problem: if a cannot fill b?
        {
            if (a >= y)
            {
                a -= y;
                b = y;
            }
            else
            {
                b = a;
                a = 0;
            }
        }
        else if (c == z)
        // if c is full, fill a with it
        // problem: if a will overflow? NO, a will never overflow!
        {
            a += z;
            c = 0;
        }
        else if (b > z-c)
        // if c is not full and fill it with b will overflow, just fill it to full
        // sometimes this won't happen bcz z > y :-)
        {
            b -= (z-c);
            c = z;
        }
        else
        // if c is not full and fill it with b is ok, just fill it
        {
            c += b;
            b = 0;
        }
        printf("%4d%4d%4d\n", a, b, c);
    }
}

int main()
{
    int x = 12, y = 8, z = 5;
    move(x, y, z);
}

